﻿Public Class frmSearchContract

    Private Sub frmSearchContract_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getHeaderGrid_Contract()
        reset()
    End Sub

    Protected Sub getHeaderGrid_Contract()
        Dim HeaderText As String() = {"លេខរៀង", "ID", "ឈ្មោះក្រុមហ៊ុន", "ឈ្មោះមេធាវី", "តម្លៃសេវាកម្មក្នុងមួយខែ", "រយៈពេល (ខែ)", "ថ្ងៃចាប់ផ្តើមកុងត្រា", "ថ្ងៃបញ្ចប់កុងត្រា"}
        Dim HeaderWidth As Integer() = {100, 100, 350, 200, 250, 150, 150, 150}
        Dim i As Integer
        dgvData.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            dgvData.Columns(i).HeaderText = HeaderText(i)
            dgvData.Columns(i).Width = HeaderWidth(i)
        Next
        dgvData.Columns(1).Visible = False
        dgvData.Columns(6).DefaultCellStyle.Format = "dd/MM/yyyy"
        dgvData.Columns(7).DefaultCellStyle.Format = "dd/MM/yyyy"
    End Sub

    Public Sub getContract_All_CaseIncome()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_All_CaseIncome()
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Public Sub getContract_Finish_CaseIncome()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_Finish_CaseIncome()
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Public Sub getContract_NotFinish_CaseIncome()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_NotFinish_CaseIncome()
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Protected Sub reset()
        txtSearch.Clear()
        txtSearch.Focus()
        rdContractIsNotFinish.Checked = True
        rdContractIsFinish.Checked = False
        rdContractAll.Checked = False
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub rdContractIsNotFinish_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdContractIsNotFinish.CheckedChanged
        RadioContract_CheckedChanged()
    End Sub

    Private Sub rdContractIsFinish_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdContractIsFinish.CheckedChanged
        RadioContract_CheckedChanged()
    End Sub

    Private Sub rdContractAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdContractAll.CheckedChanged
        RadioContract_CheckedChanged()
    End Sub

    Protected Sub RadioContract_CheckedChanged()
        If rdContractIsNotFinish.Checked Then
            getContract_NotFinish_CaseIncome()
        ElseIf rdContractIsFinish.Checked Then
            getContract_Finish_CaseIncome()
        Else
            getContract_All_CaseIncome()
        End If
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        If rdContractIsNotFinish.Checked Then
            getContract_NotFinish_CaseIncome_byKeyword()
        ElseIf rdContractIsFinish.Checked Then
            getContract_Finish_CaseIncome_byKeyword()
        Else
            getContract_All_CaseIncome_byKeyword()
        End If
    End Sub

    Public Sub getContract_All_CaseIncome_byKeyword()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_All_CaseIncome_byKeyword(txtSearch.Text)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Public Sub getContract_Finish_CaseIncome_byKeyword()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_Finish_CaseIncome_byKeyword(txtSearch.Text)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Public Sub getContract_NotFinish_CaseIncome_byKeyword()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_NotFinish_CaseIncome_byKeyword(txtSearch.Text)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click
        If dgvData.Rows.Count > 0 Then
            frmCaseIncome.cboContract.SelectedValue = dgvData.CurrentRow.Cells(1).Value
            Close()
        End If
    End Sub

    Private Sub dgvData_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvData.DoubleClick
        btnChoose_Click(Nothing, Nothing)
    End Sub
End Class